package com.example.admin.service;


import com.example.admin.common.Result;
import com.example.admin.entity.AdminLogin;
import com.example.admin.entity.Customer;
import com.example.admin.mapper.AdminMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminService {


    @Autowired
    private AdminMapper adminMapper;

    public List<Customer> getCustomerList() {
//        List<Customer> customerList = adminMapper.selectList(null);
        List<Customer> customerList = adminMapper.getCustomerList();
        return customerList;
    }

    public Result updateRole(Customer customer) {
        int count = adminMapper.updateRole(customer);
        if (count == 0) {
            return Result.error("400", "修改失败");
        }
        return Result.success("修改成功");
    }

    public Customer getCustomerByName(String customerName) {
        Customer customer = adminMapper.getCustomerName(customerName);
        return customer;
    }

    public Result deleteById(Integer id) {

        int count = adminMapper.deleteById(id);
        if (count == 0) {
            return Result.error("400", "删除失败");
        }
        return Result.success("删除成功");
    }

    public Result login(AdminLogin adminLogin) {

        AdminLogin admin = adminMapper.login(adminLogin);
        if (admin == null) {
            return Result.error("400", "登录失败");
        }

        return Result.success("登录成功");
    }

    public Result register(AdminLogin adminLogin) {

        AdminLogin login = adminMapper.register(adminLogin);
        if (login != null) {
            if (login.getUsername() != null && login.getPassword() != null) {
                return Result.error("400", "该用户已经被注册");
            }
        }
        int count = adminMapper.insertAdmin(adminLogin);
        if (count == 0) {
            return Result.error("400", "注册失败");
        }

        return Result.success("注册成功");
    }
}
